package org.cresource.org.controller;

import io.swagger.annotations.Api;
import org.cresource.basic.util.AjaxResult;
import org.cresource.basic.util.FastDfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {
    /**
     * 上传接口
     */
    @PostMapping
    public AjaxResult upload(@RequestPart(value = "file",required = true) MultipartFile file){
        try {
            System.out.println("-----------------------------"+file);
            String filename = file.getOriginalFilename();
            System.out.println("------------------------------------"+filename);
            String extName = filename.substring(filename.lastIndexOf(".") + 1);

            String path = FastDfsUtil.upload(file.getBytes(), extName);
            return AjaxResult.me().setResultObj(path);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("上传失败!"+e.getMessage());
        }
    }

    /**
     * 删除上传
     */
    @DeleteMapping
    public AjaxResult delete(@RequestParam(required = true,value = "path") String path){
        String pathTmp = path.substring(1); // goup1/xxxxx/yyyy
        String groupName =  pathTmp.substring(0, pathTmp.indexOf("/")); //goup1
        String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);// /xxxxx/yyyy
        System.out.println(groupName);
        System.out.println(remotePath);
        FastDfsUtil.delete(groupName, remotePath);
        return  AjaxResult.me();
    }
}
